<?php
class CKeywords extends Zend_Db_Table
{
	protected $_name;
	protected $_primary;

	public function __construct()
	{
		$this->_name = R_PRE."keywords";
		$this->_primary = "keywords_id";
		parent::__construct();
		
	}

	public function getAll($page, $pageNum ,$start = '')
	{
		$where = array();
		if ($start) {
			$where[] = 'start="'.$start.'"';
		}
		$where[] = 'product_num > 0';
		$sql = 'select keywords from ' . $this->_name . ' where ' . implode(' and ', $where);
		
		$offset = ($page - 1) * $pageNum;
		
		$sql .= ' order by view_num desc limit ' . $offset . ',' . $pageNum;
	
		return  $this->getAdapter()->fetchAll($sql);
	}
	
	public function getHotSearch($count = 10)
	{
		$sql = 'select keywords from ' . $this->_name . ' where product_num > 5 order by view_num desc limit ' . $count;
		$row = $this->getAdapter()->fetchAll($sql);
		foreach ($row as $v) {
			$arr[] = '<a href="'.MY_URL.'/search-detail?keyword=' .urlencode($v['keywords']). '">' . $v['keywords'] . '</a>';
		}
		$arr = array_unique($arr);
		return implode(', ', $arr);
	}
	
	public function getPageCount($pageNum, $start = '')
	{
		$where = array();
		
		if ($start) {
			$where[] = 'start="'.$start.'"';
		}
		$where[] = 'product_num > 0';
		$sql = 'select keywords from ' . $this->_name . ' where ' . implode(' and ', $where);
		
		$result = $this->getAdapter()->fetchAll($sql);
		
		return ceil(count($result) / $pageNum);
	}
	
	public function updateView($keywords)
	{
		$where = $this->getAdapter()->quoteInto('keywords = ?', $keywords);
		return $this->update(array('view_num'=>new Zend_Db_Expr('view_num+1')), $where);
	}
	
	public function getRelateKey($productId)
	{
		$table = 'zcshop_keywords';
        $sql = 'select t1.keywords from ' . $table . ' t1 join (select ROUND(RAND()*((select MAX(keywords_id) from ' . $table . ') - (select MIN(keywords_id) from ' . $table . ')) + (select MIN(keywords_id) from ' . $table . ')) as ID) as t2' . 
        		' inner join zcshop_product_keywords_related p on t1.keywords_id=p.keywords_id where t1.keywords_id >t2.ID and p.productid=' . intval($productId) . ' and t1.product_num > 5 limit 8, 20';
		
		//$sql = 'select distinct(k.keywords) from zcshop_product_keywords_related p inner join zcshop_keywords k on k.keywords_id=p.keywords_id where p.productid=' . intval($productId) . ' and k.product_num > 5 order by k.view_num desc limit 8, 20';
		$result = $this->getAdapter()->fetchAll($sql);
		if (count($result < 10)) {
			$result = $this->getAdapter()->fetchAll($sql);
		}
		if (count($result < 10)) {
			$result = $this->getAdapter()->fetchAll($sql);
		}
		return $result;
	}
	
	public function getDetailPopKey($productId)
	{
		$sql = 'select k.keywords from zcshop_product_keywords_related p inner join zcshop_keywords k on k.keywords_id=p.keywords_id where p.productid=' . intval($productId) . ' and k.product_num > 5 order by k.view_num desc limit 0, 8';
		$result = $this->getAdapter()->fetchAll($sql);
		$arr = array();
		foreach ($result as $v) {
			$arr[] = '<a href="'.MY_URL.'/search-detail?keyword=' .urlencode($v['keywords']). '">' . $v['keywords'] . '</a>';
		}
		$arr = array_unique($arr);
		return implode(', ', $arr);
	}
	
	public function getRandomKeywords($num,$keyword){
		$myCache = Zend_Registry::get('myCache');
		$count = $myCache->load('keywordsCount');
		if($count == 0 || !$count){
			$sql = 'select max(keywords_id) from zcshop_keywords';
			$count = $this->getAdapter()->fetchOne($sql);
			$myCache->save($count,'keywordsCount');
		}
		$ids = array();
		$md5Keyword = md5($keyword);
		for($i=0;$i<$num;$i++){
			$md5Keyword = md5(md5($md5Keyword).md5(pow($i,10))).md5($md5Keyword.$i);
			$value = '';
			for($j=0,$len=strlen($md5Keyword);$j<$len;$j++){
				$value.=ord($md5Keyword[$j]);
			}
			$value = substr($value,$i,10);
			$rid=($value+8682522807148012)%$count;
			$ids[]=$rid;
		}
		$idStr = "'".join("','",$ids)."'";
		$sql = 'select keywords from zcshop_keywords where keywords_id in('.$idStr.')';
		return $this->getAdapter()->fetchAll($sql);	
	}
	
	public function getAllKeywordsForBlackFriday()
	{
		$myCache = Zend_Registry::get('myCache');
		$key = $myCache->load('keywordsForBlackFriday');
		if(!$key){
			$sql = 'select keywords from zcshop_keywords where type=1 order by keywords asc';
			$key = $this->getAdapter()->fetchAll($sql);	
			$myCache->save($key,'keywordsForBlackFriday');
		}
		return $key;
	}
	
	public function getAllKeywordsForSpecial()
	{
		$myCache = Zend_Registry::get('myCache');
		$key = $myCache->load('keywordsForSpecial');
		if(!$key){
			$sql = 'SELECT keywords FROM zcshop_keywords WHERE TYPE=2 OR (`keywords` LIKE "%christmas%" and (`keywords` like "%soccer%" or `keywords` like "%jersey%" or `keywords` like "%football%")) order by keywords asc';
			$key = $this->getAdapter()->fetchAll($sql);	
			$myCache->save($key,'keywordsForSpecial');
		}
		return $key;
	}
	
	
	public function getCategoryRelateKey($categoryId)
	{
		$sql = 'select k.keywords from zcshop_category_keywords p inner join zcshop_keywords k on k.keywords_id=p.keywords_id where p.flid=' . intval($categoryId) . ' and k.product_num > 5 order by k.view_num desc limit 20';
		return $this->getAdapter()->fetchAll($sql);
	}
	
	public function divPages($pageNumber, $page, $thisfile)
	{
		if ($pageNumber > 0) {
			$pre = $page - 1;
			$next = $page + 1;
			$ye=0;
			$loop='';
			$firstAndLast='';
			$nextAndLast='';
			
			if(strpos($thisfile,'?')){
				
				$thisfile=$thisfile;
				
			}else{
				
				$thisfile=$thisfile;
				
			}
			if ($page > 30) {
				if ($page + 1 <= $pageNumber) {
					if($pageNumber-$page>2){									
						$ye = $page - 31+3;	
					}else{
						$ye = $page - 31 + 1;
					}
				} else {
					$ye = $page - 31;
				}
			}
			if ($pre > 0) {
				
				$firstAndLast = "<a class='pageStyle' href='" . $thisfile . "_1.html' title='First'>&lt;&lt;</a> 
				<a class='pageStyle' href='" . $thisfile . "_" . $pre . ".html' title='Prev'>&lt;</a>";
			
			}
			for($i = $ye; $i < $ye + 31; $i ++) {
				if ($page == ($i + 1)) {
					$loop .= "<a class='current' id='current' href='" . $thisfile . "_" . ($i + 1) . ".html'>" . ($i + 1) . "</a> ";
				} else {
					$loop .= "<a class='pageStyle' href='" . $thisfile . "_" . ($i + 1) . ".html'>" . ($i + 1) . "</a> ";
				}
				if ($i == ($pageNumber - 1)) {
					break;
				}
			}
			
			if ($next <= $pageNumber) {
				
				$nextAndLast = "<a class='pageStyle' href='" . $thisfile . "_" . $next . ".html' title='Next'>&gt;</a> 
				<a class='pageStyle' href='" . $thisfile . "_" . $pageNumber . ".html' title='Last'>&gt;&gt;</a>";
			
			}
			return $firstAndLast . ' ' . $loop . ' ' . $nextAndLast;
		} else {
			return "";
		}
	}
	
	public function encode($name, $size = 0)
	{
		$name = str_replace('/', ' ', $name);
		$name = str_replace('\\', ' ', $name);
		return rawurlencode(str_replace(' ', '-', $name));
	}
}